<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="商家名称" class="movement">
          <el-input
            v-model="formInline.company_name"
            placeholder="请输入商家名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品名称" class="movement">
          <el-input
            v-model="formInline.goods_name"
            placeholder="请输入商品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="推广时间">
          <el-date-picker
            v-model="formInline.time"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            @change="timeChange"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="店铺状态" class="movement">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search()" class="movement"
            >查询</el-button
          >
          <el-button type="primary" @click="resert()" class="movement"
            >重置</el-button
          >
          <el-button type="primary" @click="addTuiGuang()">新增推广</el-button>
        </el-form-item>
      </el-form>
      <!-- <el-button type="primary" class="divMia" @click="newly"
        >新增分店</el-button
      > -->
    </div>
    <div class="text item">
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
        max-height="600"
      >
        <el-table-column
          label="序号"
          align="center"
          type="index"
          :index="indexMethod"
          width="80"
        >
        </el-table-column>
        <el-table-column align="center" label="商家信息" width="300">
          <template slot-scope="scope">
            <div class="modality">
              <el-image
                style="width: 70px; height: 70px; width: 20%"
                :src="imgHeader + scope.row.seller.logo"
                class="leftImg"
              ></el-image>
              <div class="modalityUser">
                <el-tooltip
                  class="item"
                  effect="dark"
                  :content="scope.row.seller.company_name"
                  placement="top-start"
                >
                  <span class="text"
                    >公司名称:{{ scope.row.seller.company_name }}</span
                  >
                </el-tooltip>

                <el-tooltip
                  class="item"
                  effect="dark"
                  :content="scope.row.seller.shop_name"
                  placement="top-start"
                >
                  <span class="text"
                    >商家名称:{{ scope.row.seller.shop_name }}</span
                  >
                </el-tooltip>
                <el-tooltip
                  class="item"
                  effect="dark"
                  :content="scope.row.seller.seller_type_name"
                  placement="top-start"
                >
                  <span class="text"
                    >商家类型:{{ scope.row.seller.seller_type_name }}</span
                  >
                </el-tooltip>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="goods.name" align="center" label="推广商品名称">
        </el-table-column>
        <el-table-column align="center" label="商品图片" width="120">
          <template slot-scope="scope">
            <el-image
              style="width: 80px; height: 80px"
              :src="imgHeader + scope.row.goods.img.split(',')[0]"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column>

        <el-table-column align="center" label="推广时间" width="160">
          <template slot-scope="scope">
            {{ scope.row.start_time }} - {{ scope.row.end_time }}
          </template>
        </el-table-column>
        <el-table-column
          prop="seller_shop.manager_name"
          align="center"
          label="店铺负责人"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.seller.store_manager_name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="seller_shop.manager_phone"
          align="center"
          label="负责人电话"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.seller.store_manager_phone }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status_en" align="center" label="状态">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 1"
              type="danger"
              size="mini"
              @click="handleEdit(scope.row)"
              >终止推广</el-button
            >
            <el-button
              v-if="scope.row.status == 0"
              type="success"
              size="mini"
              @click="handleEdit(scope.row)"
              >恢复推广</el-button
            >
            <el-button
              v-if="scope.row.status == 2"
              type="danger"
              size="mini"
              @click="sanChu(scope.row)"
              >删除</el-button
            >
            <el-button
              type="primary"
              size="mini"
              @click="handleDetails(scope.row)"
              >查看</el-button
            >
            <el-button
              type="warning"
              size="mini"
              @click="handleDelete(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 100, 200, 300]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="titleText"
      :visible.sync="dialogVisible"
      width="45%"
      :before-close="handleClose"
    >
      <span>
        <el-form
          ref="form"
          :model="formLabelAlign"
          label-width="90px"
          :rules="rules"
        >
          <!-- <div class="divWidth">
            <el-form-item label="商家名称">
              <el-input
                v-model="formLabelAlign.seller.company_name"
                placeholder="请输入商家名称"
                :disabled="disabled2"
              ></el-input>
            </el-form-item>
          </div> -->
          <div class="divWidth">
            <el-form-item label="商家名称" prop="seller.shop_name">
              <el-select
                v-model="formLabelAlign.seller.shop_name"
                filterable
                placeholder="请选择"
                :disabled="disabled2"
                @change="handleChange1"
              >
                <el-option
                  v-for="item in shangJia"
                  :key="item.id"
                  :label="item.company_name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </div>

          <div class="divWidth">
            <el-form-item label="负责人">
              <el-input
                v-model="formLabelAlign.seller.store_manager_name"
                placeholder="请输入负责人"
                :disabled="disabled"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="负责人电话">
              <el-input
                v-model="formLabelAlign.seller.store_manager_phone"
                placeholder="请输入负责人电话"
                :disabled="disabled"
              ></el-input>
            </el-form-item>
          </div>
          <!-- <div class="divWidth">
            <el-form-item label="商品名称">
              <el-input
                v-model="formLabelAlign.goods.name"
                placeholder="请输入商品名称"
                :disabled="disabled2"
              ></el-input>
            </el-form-item>
          </div> -->
          <div class="divWidth">
            <el-form-item label="商品名称" prop="goods.name">
              <el-select
                v-model="formLabelAlign.goods.name"
                filterable
                placeholder="请选择"
                :disabled="disabled2"
                @change="handleChange2"
              >
                <el-option
                  v-for="item in shangPin"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="商品类型1">
              <el-input
                v-model="formLabelAlign.goods.category"
                placeholder="请输入商品类型"
                :disabled="disabled"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="商品品牌">
              <el-input
                v-model="formLabelAlign.goods.brand"
                placeholder="请输入商品品牌"
                :disabled="disabled"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="商品编号">
              <el-input
                v-model="formLabelAlign.goods_id"
                placeholder="请输入商品编号"
                :disabled="disabled"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="商品图片">
              <!-- <el-upload
                class="upload-demo"
                action
                :http-request="uploadFiles"
                ref="upload"
                :limit="fileLimit"
                :on-remove="handleRemove"
                :file-list="fileList"
                :on-exceed="handleExceed"
                :before-upload="beforeUpload"
                :show-file-list="false"
                :headers="headers"
                multiple
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过5M
                </div>
              </el-upload> -->
              <!-- <ul class="el-upload-list el-upload-list--picture-card">
                <draggable v-model="imgShowList">
                  <li
                    v-for="(item, index) in imgShowList"
                    :key="index"
                    class="el-upload-list__item is-success animated"
                  >
                    <img
                      :src="'https://bjsf.bjsky.top/' + item"
                      alt=""
                      class="el-upload-list__item-thumbnail"
                    />
                    <i class="el-icon-close"></i>
                    <span class="el-upload-list__item-actions">
                      <span
                        class="el-upload-list__item-preview"
                        @click="handlePictureCardPreviewFileDetail(item)"
                      >
                        <i class="el-icon-zoom-in"></i>
                      </span>
                      <span
                        class="el-upload-list__item-delete"
                        @click="handleRemoveFileDetail(index)"
                      >
                        <i class="el-icon-delete"></i>
                      </span>
                    </span>
                  </li>
                </draggable>
              </ul> -->
              <template>
                <el-image
                  style="width: 80px; height: 80px"
                  :src="imgHeader + formLabelAlign.goods.img"
                  fit="cover"
                ></el-image>
              </template>
            </el-form-item>
          </div>
          <p class="setP">推广设置</p>
          <div class="divWidth">
            <el-form-item label="推广模块" prop="module">
              <el-cascader
                :options="options3"
                :props="{ checkStrictly: true, emitPath: false }"
                clearable
                v-model="formLabelAlign.module"
              ></el-cascader>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="推广类型" prop="type">
              <el-select v-model="formLabelAlign.type" placeholder="请选择">
                <el-option
                  v-for="item in options1"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="推广排序" prop="sort">
              <el-input
                v-model="formLabelAlign.sort"
                placeholder="请输入排序"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="推广时间" prop="time">
              <el-date-picker
                v-model="formLabelAlign.time"
                type="datetimerange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
              >
              </el-date-picker>
            </el-form-item>
            <!-- <el-form-item label="推广时间" prop="time">
              <el-date-picker
                v-model="formLabelAlign.time"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
              >
              </el-date-picker>
            </el-form-item> -->
          </div>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">返回</el-button>
        <el-button type="primary" @click="save">确 认</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="titleText"
      :visible.sync="dialogVisible2"
      width="45%"
      :before-close="handleClose2"
    >
      <span>
        <el-form ref="form" :model="formLabelAlign2" label-width="90px">
          <div class="divWidth">
            <el-form-item label="商家名称">
              <el-input
                v-model="formLabelAlign2.seller.shop_name"
                placeholder="请输入商家名称"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="负责人">
              <el-input
                v-model="formLabelAlign2.seller.store_manager_name"
                placeholder="请输入负责人"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="负责人电话">
              <el-input
                v-model="formLabelAlign2.seller.store_manager_phone"
                placeholder="请输入负责人电话"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="商品名称">
              <el-input
                v-model="formLabelAlign2.goods.name"
                placeholder="请输入商品名称"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="商品类型">
              <el-input
                v-model="formLabelAlign2.goods.category"
                placeholder="请输入商品类型"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="商品品牌">
              <el-input
                v-model="formLabelAlign2.goods.brand"
                placeholder="请输入商品品牌"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="商品编号">
              <el-input
                v-model="formLabelAlign2.goods_id"
                placeholder="请输入商品编号"
                :disabled="true"
              ></el-input>
            </el-form-item>
          </div>
          <div class="divWidth">
            <el-form-item label="商品图片">
              <template>
                <el-image
                  style="width: 80px; height: 80px"
                  :src="imgHeader + formLabelAlign2.goods.img"
                  fit="cover"
                ></el-image>
              </template>
            </el-form-item>
          </div>
        </el-form>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  uploadFiles,
  storeList,
  addShop,
  region,
  storeStatus,
  shopDetails,
  StoreDeletion,
  condition,
  productList,
  merchant,
} from "../../../../utils/api.js";
import {
  getGongZhong,
  addGongZhong,
  editGongZhong,
  delGongZhong,
  QiYong,
  getPromotion,
  getPromotionDeatil,
  addPromotion,
  delPromotion,
  restorePromotion,
  getClassification,
  editPromotion,
} from "../../../../utils/main.js";
export default {
  data() {
    return {
      headers: { "Content-Type": "multipart/form-data" },
      radio: 2,
      options3: [],
      imgHeader: this.Global.imgHeader,

      options: [
        {
          value: "0",
          label: "终止推广",
        },
        {
          value: "1",
          label: "推广中",
        },
        {
          value: "2",
          label: "已结束",
        },
        {
          value: "3",
          label: "待推广",
        },
      ],
      options1: [
        {
          id: 1,
          name: "新品",
        },
        {
          id: 2,
          name: "热卖",
        },
      ],
      options5: [
        // {
        //   value: "选项1",
        //   label: "黄金糕",
        // },
        // {
        //   value: "选项2",
        //   label: "双皮奶",
        // },
        // {
        //   value: "选项3",
        //   label: "蚵仔煎",
        // },
        // {
        //   value: "选项4",
        //   label: "龙须面",
        // },
        // {
        //   value: "选项5",
        //   label: "北京烤鸭",
        // },
        // {
        //   value: "选项6",
        //   label: "黄金纸杯蛋糕",
        // },
      ],
      value: "",
      formInline: {
        company_name: "",
        goods_name: "",
        status: "",
        time: "",
      },
      formLabelAlign: {
        id: "",
        sort: "",
        goods_id: "",
        status: "",
        time: "",
        notice: "",
        module: "",
        type: "",
        goods: {
          id: "",
          img: "",
          name: "",
          brand: "",
        },
        seller: {
          company_name: "",
          shop_name: "",
          store_manager_name: "",
          store_manager_phone: "",

          id: "",
          logo: "",
          seller_type: "",
        },
        seller_shop: {
          id: 1,
          manager_name: "",
          manager_phone: "",
          name: "",
        },
      },
      formLabelAlign2: {
        sort: "",
        goods_id: "",
        status: "",
        time: "",
        notice: "",
        type: "",
        goods: {
          id: "",
          img: "",
          name: "",
        },
        seller: {
          company_name: "",
          name: "",
          store_manager_name: "",
          store_manager_phone: "",
          id: "",
          logo: "",
          seller_type: "",
        },
        seller_shop: {
          id: 1,
          manager_name: "",
          manager_phone: "",
          name: "",
        },
      },
      titleText: "",
      imgShowList: [],
      fileLimit: 1,
      loading: false,
      token: sessionStorage.getItem("token"),
      dialogVisible: false,
      dialogVisible2: false,
      disabled: false,
      disabled2: false,
      fileList: [],
      tableData: [],
      datailData: [],
      //当前页数
      page: 1,
      limit: 10,
      total: 0,
      currentPage: 1,
      status: "",
      name: "",
      time: "",
      type: "",
      company_name: "",
      shop_name: "",
      goods_name: "",
      id: "",
      rules: {
        "seller.shop_name": [
          { required: true, message: "请选择商家名称", trigger: "blur" },
        ],
        "goods.name": [
          { required: true, message: "请选择商品名称", trigger: "blur" },
        ],
        module: [
          { required: true, message: "请选择推广模块", trigger: "blur" },
        ],
        type: [{ required: true, message: "请选择推广类型", trigger: "blur" }],
        sort: [
          { required: true, message: "请输入排序", trigger: "blur" },
          {
            validator: function (rule, value, callback) {
              if (/^\d+$/.test(value) == false) {
                callback(new Error("请输入不小于0的整数"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        time: [{ required: true, message: "请输入推广时间", trigger: "blur" }],
      },
      productList: {
        name: "",
        goods_no: "",
        brand_id: "",
        visibility: "",
        origin: "",
        category_ids: "",
        seller_shop_id: "",
        goods_id: "",
        page: "",
        pageSize: "",
      },
      productList2: {
        company_name: "",
        seller_type: "",
        store_manager_name: "",
        store_manager_phone: "",
        is_lock: "",
        id: "",
        pageSize: "",
        page: "",
      },
      // 筛选结果
      business: null,
      commodity: null,
      shangPin: [],
      shangJia: [],
    };
  },
  methods: {
    timeChange(e) {
      if (this.formInline.time == null) {
        this.formInline.time == "";
        this.time = "";
      } else {
        this.time = this.formInline.time;
      }

      this.getList();
    },
    // 获取分类列表
    async getSelect() {
      // this.loading = true;
      let res = await getClassification(this.token)
        .then((res) => {
          if (res.data.status) {
            this.options3 = res.data.data;
            // console.log(this.options3);
            // let timer = setTimeout(() => {
            //   this.loading = false;
            //   clearTimeout(timer);
            // }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            // let timer = setTimeout(() => {
            //   this.$message.error(res.data.msg);
            // }, 5000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    // 恢复、终止推广接口
    conditionUser(id, status) {
      restorePromotion(this.token, id, status)
        .then((res) => {
          if (res.data.status) {
            this.getList();
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },

    // 获取推广商品列表
    async getList() {
      this.loading = true;
      let res = await getPromotion(
        this.token,
        this.company_name,
        this.shop_name,
        this.goods_name,
        this.time,
        this.type,
        this.status,
        this.limit,
        this.page
      );
      if (res.data.status) {
        this.tableData = res.data.data.data || [];
        this.total = res.data.data.total;
        // console.log(this.total);
        // console.log(this.tableData, "55555");
        let timer = setTimeout(() => {
          this.loading = false;
          clearTimeout(timer);
        }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/login");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    search() {
      this.company_name = this.formInline.company_name;
      this.goods_name = this.formInline.goods_name;
      if (this.formInline.time == null) {
        this.formInline.time = "";
      }
      this.time = this.formInline.time;
      this.status = this.formInline.status;
      this.page = 1;
      this.getList();
    },
    resert() {
      this.formInline.company_name = "";
      this.formInline.goods_name = "";
      this.formInline.time = "";
      this.formInline.status = "";
      this.company_name = "";
      this.goods_name = "";
      this.time = "";
      this.status = "";
      this.getList();
    },
    // 恢复/终止推广
    handleEdit(row) {
      if (row.status == 0) {
        let status = 1;
        this.conditionUser(row.id, status);
      }
      if (row.status == 1) {
        let status = 0;
        this.conditionUser(row.id, status);
      }
      // this.getList();
    },
    // 详情
    handleDetails(row) {
      this.dialogVisible2 = true;
      let obj = JSON.parse(JSON.stringify(row));
      this.formLabelAlign2 = obj;
      this.formLabelAlign2.goods.img = obj.goods.img.split(",")[0];
      // console.log(this.formLabelAlign2);
      this.id = row.id;
      // console.log(this.id);
      this.getDetail();
    },
    // 获取详情信息
    async getDetail() {
      this.loading = true;
      let res = await getPromotionDeatil(this.token, this.id);
      if (res.data.status) {
        this.datailData = res.data.data.data || [];
        // console.log(this);
        // this.tableData.forEach(item=>{
        //   item.allprice = item.nums*item.price
        // })
        // this.total = res.data.data.count;
        // console.log(this.total, shouchu);
        let timer = setTimeout(() => {
          this.loading = false;
          clearTimeout(timer);
        }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/login");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },

    //新增
    addTuiGuang() {
      this.formLabelAlign.seller.shop_name = "";
      this.formLabelAlign.seller.store_manager_name = "";
      this.formLabelAlign.seller.store_manager_phone = "";
      this.formLabelAlign.goods.name = "";
      this.formLabelAlign.goods.category = "";
      this.formLabelAlign.goods.brand = "";
      (this.formLabelAlign.sort = ""), (this.formLabelAlign.goods_id = "");
      this.formLabelAlign.module = "";
      this.formLabelAlign.type = "";
      // this.formLabelAlign.sort = "";
      this.formLabelAlign.goods.img = "";
      this.formLabelAlign.time = "";
      this.dialogVisible = true;
      this.disabled2 = false;
      this.titleText = "新增商品推广";
      this.disabled = true;
    },
    // 编辑
    handleDelete(row) {
      this.disabled = true;
      this.disabled2 = true;
      let obj = JSON.parse(JSON.stringify(row));
      this.formLabelAlign = obj;
      this.formLabelAlign.goods.img = obj.goods.img.split(",")[0];
      //时间回显处理 将data 返回的时间push进去
      let tmpArr = [];
      tmpArr.push(new Date(obj.start_time));
      tmpArr.push(new Date(obj.end_time));
      this.formLabelAlign.time = tmpArr;
      // console.log(this.formLabelAlign);
      this.dialogVisible = true;
      // if (row.type === 1) {
      //   this.formLabelAlign.type = "新品";
      // } else {
      //   this.formLabelAlign.type = "热卖";
      // }
      this.titleText = "编辑商品推广";
    },

    // 排序
    indexMethod(index) {
      if (this.token < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.currentPage - 1) * this.limit - index;
      }
    },

    //关闭新增和编辑窗口
    handleClose() {
      // (this.imgShowList = []), (this.imgShowForm = []), (this.form = {});
      // done();
      // this.regions1();
      this.$refs.form.resetFields();
      this.dialogVisible = false;
      // this.getList();
    },
    //关闭详情窗口
    handleClose2() {
      // (this.imgShowList = []), (this.imgShowForm = []), (this.form = {});
      // done();
      // this.regions1();
      this.dialogVisible2 = false;
      // this.getList();
    },
    //删除
    sanChu(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // console.log(row.id);
          delPromotion(this.token, row.id).then((res) => {
            if (res.data.status) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getList();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 判断日期时间类型是否是 2023-02-10 17:12:12
    isDataType(str) {
      var dateTimess = str
        .toString()
        .match(
          /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/
        );
      // console.log("dateTimess", dateTimess); //如果不是 2023-02-10 17:12:12这种类型返回null
      return dateTimess;
    },
    // 将时日期式转变为2023-02-10 17:12:12格式
    convertDates(arr) {
      const result = [];
      const resultTime = [];
      const datePattern =
        /^([a-zA-Z]{3}\s+\d{1,2}\s+\d{4}\s+\d{2}:\d{2}:\d{2}\s+GMT[+-]\d{4}\s+\(GMT[+-]\d{4}\))?$/;
      arr.forEach((item) => {
        console.log("item", item);
        item = item.toString();
        // 解析并转换日期时间格式
        const [_, month, day, year, hour, minute, second, tz] = item.match(
          /(\w+)\s+(\d+)\s+(\d+)\s+(\d+:\d+:\d+)\s/
        );
        const months = [
          "Jan",
          "Feb",
          "Mar",
          "Apr",
          "May",
          "Jun",
          "Jul",
          "Aug",
          "Sep",
          "Oct",
          "Nov",
          "Dec",
        ];
        const monthIndex = months.indexOf(month);
        const formattedDate = `${year}-${
          monthIndex + 1 > 9 ? monthIndex + 1 : "0" + (monthIndex + 1)
        }-${day.padStart(2, "0")} ${hour}:${minute}:${second}`;
        result.push(formattedDate);
        // console.log("formattedDate", formattedDate);
      });
      result.map((timeStr) => {
        // 使用正则表达式移除':undefined'和最后一个':'及其后的所有字符
        timeStr = timeStr.replace(/:undefined:.+$/, "");
        // console.log("timeStr", timeStr);
        resultTime.push(timeStr);
        // 或者使用slice方法，假设时间格式固定长度，直接截取前14+6位
        // return timeStr.slice(0, 111);
      });
      return resultTime;
    },
    //确定添加
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.isDataType(this.formLabelAlign.time[0]) == null) {
            this.formLabelAlign.time = this.convertDates(
              this.formLabelAlign.time
            );
          }
          // console.log(this.formLabelAlign, "8888880");
          if (this.formLabelAlign.id != "") {
            // console.log(this.formLabelAlign, "000");
            // 编辑
            editPromotion(
              this.token,
              this.formLabelAlign.id,
              this.formLabelAlign.module,
              this.formLabelAlign.type,
              this.formLabelAlign.sort,
              this.formLabelAlign.time,
              2
            ).then((res) => {
              if (res.data.status) {
                this.$message({
                  message: "修改成功",
                });
                this.dialogVisible = false;
                this.getList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            // console.log(this.formLabelAlign, "111");
            // 新增
            addPromotion(
              this.token,
              this.formLabelAlign.goods_id,
              this.formLabelAlign.module,
              this.formLabelAlign.type,
              this.formLabelAlign.sort,
              this.formLabelAlign.time,
              2
            ).then((res) => {
              if (res.data.status) {
                this.$message({
                  message: "新增成功",
                });
                // this.total = res.data.data.count;
                // this.formLabelAlign = {};
                this.dialogVisible = false;
                this.getList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
    },

    //页数
    handleSizeChange(val) {
      this.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },

    handleRemove() {},
    //这是我自定义的移除事件
    handleClosedelete(i) {
      // this.fileList.splice(i, 1);//删除上传的文件
    },
    // uploadFiles(item) {
    //   this.$message("文件上传中........");
    //   //上传文件的需要formdata类型;所以要转
    //   uploadFiles(item.file).then((res) => {
    //     if (res.data.status != false || res.data.id != null) {
    //       res.data.data.saveurl; //半截
    //       res.data.data.weburl; //全拼
    //       // this.form.logo1 = res.data.data.saveurl;
    //       // this.form.logo = res.data.data.weburl;
    //       this.fileList.push(item.file); //成功过后手动将文件添加到展示列表里
    //       let i = this.fileList.indexOf(item.file);
    //       this.handleSuccess();
    //     }
    //   });
    // },
    //上传文件的格式
    // beforeUpload(file) {
    //   if (file.type != "" || file.type != null || file.type != undefined) {
    //     //截取文件的后缀，判断文件类型
    //     const FileExt = file.name.replace(/.+\./, "").toLowerCase();
    //     //计算文件的大小
    //     const isLt5M = file.size / 1024 / 1024 < 1; //这里做文件大小限制
    //     //如果大于50M
    //     if (!isLt5M) {
    //       this.$message.error("上传文件大小不能超过 1MB!");
    //       return false;
    //     }
    //     //如果文件类型不在允许上传的范围内
    //     if (this.fileType.includes(FileExt)) {
    //       return true;
    //     } else {
    //       this.$message.error("上传文件格式不正确!");
    //       return false;
    //     }
    //   }
    // },
    // //超出文件个数的回调
    // handleExceed() {
    //   this.$message({
    //     type: "warning",
    //     message: "超出最大上传文件数量的限制！",
    //   });
    //   return;
    // },
    // 商品列表
    // productLists() {
    //   this.loading = true;
    //   productList(
    //     this.productList.name,
    //     this.productList.goods_no,
    //     this.productList.brand_id,
    //     this.productList.visibility,
    //     this.productList.origin,
    //     this.productList.category_ids,
    //     this.productList.seller_shop_id,
    //     this.productList.goods_id,
    //     this.productList.page,
    //     this.productList.pageSize
    //   )
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.shangPin = res.data.data.data;
    //         // console.log(this.shangPin);
    //         // this.total = res.data.data.total;
    //         // this.tableData.forEach((item) => {
    //         //   if (item.brand) {
    //         //     item.brand_id = item.brand.name;
    //         //   }
    //         //   if (item.img) {
    //         //     item.imgShow = item.img.split(",")[0];
    //         //     // console.log(item.img.split(",")[0]);
    //         //     // console.log(item.img.split(","));
    //         //   }
    //         //   // console.log(item);
    //         //   item.categorys.forEach((item1) => {
    //         //     if (item1.category != null) {
    //         //       this.categoryS = item1.category;
    //         //       item.categoryTS = this.categoryS.name;
    //         //     }
    //         //   });
    //         // });
    //         // let timer = setTimeout(() => {
    //         //   this.loading = false;
    //         //   clearTimeout(timer);
    //         // }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         let timer = setTimeout(() => {
    //           this.$message.error(res.data.msg);
    //         }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    //商家列表
    merchants() {
      this.loading = true;
      merchant(
        this.token,
        this.productList2.company_name,
        this.productList2.seller_type,
        this.productList2.store_manager_name,
        this.productList2.store_manager_phone,
        this.productList2.is_lock,
        this.productList2.id,
        this.productList2.pageSize,
        this.productList2.page
      )
        .then((res) => {
          if (res.data.status) {
            this.shangJia = res.data.data.data;
            // this.total = res.data.data.total;
            // this.tableData.forEach((item) => {
            //   if (item.logo) {
            //     item.logo1 = item.logo;
            //     item.logo = "https://bjsf.bjsky.top" + item.logo;
            //   }
            // });
            // this.tableData.forEach((item) => {
            //   if (item.seller_type == 1) {
            //     item.seller_type_text = "主材";
            //   }
            //   if (item.seller_type == 2) {
            //     item.seller_type_text = "辅材";
            //   }
            // });
            // this.tableData.forEach((item) => {
            //   if (item.is_lock == 0) {
            //     item.is_lock_text = "启用";
            //   }
            //   if (item.is_lock == 1) {
            //     item.is_lock_text = "停用";
            //   }
            // });
            // let timer = setTimeout(() => {
            //   this.loading = false;
            //   clearTimeout(timer);
            // }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },

    // 商家名称选择框改变事件
    async handleChange1() {
      const id = this.formLabelAlign.seller.shop_name;
      await merchant(
        this.token,
        this.productList2.company_name,
        this.productList2.seller_type,
        this.productList2.store_manager_name,
        this.productList2.store_manager_phone,
        this.productList2.is_lock,
        id,
        this.productList2.pageSize,
        this.productList2.page
      ).then((res) => {
        // console.log(res);
        this.business = res.data.data.data[0];
      });
      this.formLabelAlign.seller.store_manager_name =
        this.business.store_manager_name;
      this.formLabelAlign.seller.store_manager_phone =
        this.business.store_manager_phone;
      // 商品
      this.formLabelAlign.goods.name = "";
      this.formLabelAlign.goods.category = "";
      this.formLabelAlign.goods.brand = "";
      this.formLabelAlign.goods_id = "";
      this.formLabelAlign.goods.img = "";
      let seller_shop_id = this.business.id;
      await productList(
        this.token,
        this.productList.name,
        this.productList.goods_no,
        this.productList.brand_id,
        1,
        this.productList.origin,
        this.productList.category_ids,
        seller_shop_id,
        this.productList.goods_id,
        this.productList.page,
        this.productList.pageSize,
        this.business.account_by_seller.seller_id,
        ""
      ).then((res) => {
        // console.log(res);
        this.shangPin = res.data.data.data;
      });
      // console.log(this.shangPin);
    },
    // 商品名称选择框改变事件
    async handleChange2() {
      const goods_id = this.formLabelAlign.goods.name;
      await productList(
        this.token,
        this.productList.name,
        this.productList.goods_no,
        this.productList.brand_id,
        this.productList.visibility,
        this.productList.origin,
        this.productList.category_ids,
        this.productList.seller_shop_id,
        goods_id,
        this.productList.page,
        this.productList.pageSize,
        this.business.account_by_seller.seller_id,
        ""
      ).then((res) => {
        // console.log(res);
        this.commodity = res.data.data.data[0];
      });
      console.log("000000", this.commodity.categorys[0].name);
      this.formLabelAlign.goods.category =
        this.commodity.categorys[0].category.name;
      this.formLabelAlign.goods.brand = this.commodity.brand.name;
      this.formLabelAlign.goods_id = this.commodity.id;
      this.formLabelAlign.img = this.commodity.img;
      this.formLabelAlign.goods.img = this.commodity.img.split(",")[0];
      // this.formLabelAlign.goods_id = this.commodity.img;
      // console.log(this.formLabelAlign.goods.brand);
    },
  },
  mounted() {
    // this.storeLists();
    // this.regions1();
    this.getList();
    this.getSelect();
    // this.productLists();
    this.merchants();
    // this.form.seller_id = this.$route.params.id;
  },
};
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

/* .clearfix {
  text-align: right;
} */

.box-card {
  width: 100%;
}
.dialog-footer {
  width: 100%;
  display: flex;
  justify-content: center;
}

.imageGood {
  margin: 0 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.divWidth {
  width: 70%;
}
.modality {
  display: flex;
  width: 100%;
}

.modality .leftImg {
  width: 20%;
}
.modalityUser {
  display: flex;
  flex-direction: column;
  margin: 0 15px;
  width: 70%;
  /* height: 70px; */
  /* justify-content: space-between;
  border: 1px solid pink; */
}

.modalityUser .item {
  display: block;
  text-align: left;
  width: 90%;
  /* height: 30%; */
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  margin-bottom: 0;
  /* border: 1px solid black; */
}
.addImg {
  display: flex;
  /* border: 1px solid black; */
  height: 100px;
  justify-content: space-between;
}
.addImg img {
  width: 30%;
  border: 1px solid pink;
}
.setP {
  font-size: 18px;
  /* font-weight: 700; */
  margin-bottom: 10px;
}
textarea {
  padding: 10px;
}
</style>